<template>
  <div class="login">
    <!-- <div>{{this.$route.query.openid}}分享人id</div>-->
    <!-- <div>{{this.$route.query.url}}</div> -->
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import axios from 'axios'
import qs from 'qs'
import { setTimeout } from 'timers'
export default {
  data () {
    return {
      formInline: true,
      code: '',
      state: '',
      regAppid: '',
      theRequest: {},
      locationUrl: '',
      theOther: ''
    }
  },
  created () {
    this.$store.commit('updateLoadingStatus', { isLoading: false })
    // 跳转授权
    this.locationUrl =
      'http://web.zhiyunzaixian.com/wechat/Authorize/get_authorize_url?app_id=' +
      this.$store.state.app_id +
      '&url=' +
      this.$store.state.url

    // 获取url中"?"符后的字串
    let url = window.location.search
    // console.log(this.$route.query)
    if (url.indexOf('?') !== -1) {
      // 获得url中的参数
      let str = url.substr(1)
      let strs = str.split('&')
      for (let i = 0; i < strs.length; i++) {
        this.theRequest[strs[i].split('=')[0]] = unescape(
          strs[i].split('=')[1]
        )
      }
    }
    console.log(url)
    console.log(1111111111)

    // if(this.theRequest.data){
    // Cookies.set('activity_index',1)
    // console.log('cookies')
    // }

    if (this.theRequest.data) {
      Cookies.set('activity_index', 1)
    }

    if (this.theRequest.shareid) {
      Cookies.set('theRequest', this.theRequest)
    }
    // setTimeout(() => {
    // console.log('进入')
    //     this.$router.push({
    //       path: '/home'
    //     })
    // }, 3000)

    // setTimeout(() => {
    this.shouquan()
    // }, 3000)
  },
  methods: {
    shouquan () {
      // if (this.theRequest.shareDefault == 'fenxiang') { // hash模式专属方法
      //   this.theRequest.code = ''
      // }
      console.log(this.theRequest)
      console.log(22222222222)

      if (this.theRequest.code === undefined || this.theRequest.code === '') {
        window.location = this.locationUrl
      } else {
        this.get_url()
      }
    },
    get_url () {
      // }
      console.log(this.theRequest) // }
      console.log(3333333333333)
      if (Cookies.get('theRequest')) {
        this.theOther = JSON.parse(Cookies.get('theRequest'))
      }

      let params = {
        appid: this.theRequest.appid,
        state: this.theRequest.state,
        code: this.theRequest.code,
        pid: this.theOther.shareid !== undefined && this.theOther.shareid !== '' ? this.theOther.shareid : 0
      }
      axios
        .post(
          'http://web.zhiyunzaixian.com/wechat/Authorize/get_url_s',
          qs.stringify(params)
        )
        .then(res => {
          let { code, data } = res.data
          if (code === 200) {
            this.$store.commit('member_id', data.member_id)
            this.$store.commit('openid', data.encode_member_id)
            // this.$store.commit("url", Cookies.get("url"));
            Cookies.set('theRequest', '')
            if (this.theOther.shareid) { // 跳转分享页面
              this.$router.push({
                name: this.theOther.url,
                query: {
                  cid: JSON.stringify(this.theOther)
                }
              })
            } else {
              // console.log(Cookies.get('activity_index'))
              // if(Cookies.get('activity_index') == 1){
              //
              // this.$router.push({
              //   path: '/activity'
              // })
              // }else{
              // this.$router.push({
              //  path: '/activity_index'
              // })
              // }
              // this.$router.push({
              //   path: '/home'
              // })
            }
          } else {
            // window.location = this.locationUrl;
          }
        })
    }
  }
}
</script>

<style scoped>
.login {
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: #333333;
}
</style>
